<!--评分对话框-->
<script setup lang="ts">


import { getCurrentInstance, reactive, ref } from "vue";
import { FormInstance } from "element-plus";
import { reqScoring } from "@/api/credit/peasants/score";
import { reqBaseData } from "@/api/type";


const ruleFormRef1 = ref<FormInstance>();
const ruleFormRef2 = ref<FormInstance>();
const title = ref("信用评分");
const dialogVisible = ref(false);

const from = reactive({
  scoreId: undefined,//评议ID
  hzId: undefined,

  gamble: undefined, // 是否有赌博行为
  drugRelated: undefined, // 是否有涉毒行为
  whoring: undefined,     // 是否有嫖娼行为
  black: undefined,     // 是否有涉黑行为
  privateLending: undefined,     // 是否有参与民间借贷行为
  onlineLoan: undefined,     // 是否有参与网贷行为

  loveCountry: undefined, //爱党爱国
  aggresively: undefined, // 积极上进
  familyRship: undefined, // 家庭关系
  inteConcept: undefined, // 诚信观念
  remark: undefined, // 其他说明

});

let $emit = defineEmits(["getList"]);
const { proxy }: any = getCurrentInstance();
const active = ref(0)
const showInfo = reactive({
  scoreId: undefined,
  hzId: undefined,
  hzName: undefined,
  hzIdCard: undefined,
  famAddress: undefined,
})

// 对外暴露
const show = (item: any = {}) => {
  // 获取农户的Id,省份证好，家庭住址，和姓名
  showInfo.hzId = item.hzBaseDto.hzId;
  showInfo.hzName = item.hzBaseDto.hzName;
  showInfo.hzIdCard = item.hzBaseDto.hzIdCardNumber;
  showInfo.famAddress = item.hzBaseDto.familyAddress;

  from.hzId = item.hzId;
  from.scoreId = item.scoreId;
  console.log(121);
  // 打开对话框
  setTimeout(() => {
    dialogVisible.value = true;
  }, 500);
};


// 关闭对话框后的回调
const close = () => {
  ruleFormRef1.value.resetFields();
  ruleFormRef2.value.resetFields();
  Object.keys(from).forEach((key) => {
    from[key] = undefined;
  });
  active.value = 0;
};

const rules = ref({
  gamble: [{
    required: true,
    message: "本人及家庭成员是否有赌博行为不能为空",
    trigger: "change"
  }],
  drugRelated: [{
    required: true,
    message: "本人及家庭成员是否有涉毒行为不能为空",
    trigger: "change"
  }],
  whoring: [{
    required: true,
    message: "本人及家庭成员是否有嫖娼行为不能为空",
    trigger: "change"
  }],
  black: [{
    required: true,
    message: "本人及家庭成员是否有涉黑行为不能为空",
    trigger: "change"
  }],
  privateLending: [{
    required: true,
    message: "本人及家庭成员是否有参有民间借贷不能为空",
    trigger: "change"
  }],
  onlineLoan: [{
    required: true,
    message: "本人及家庭成员是否有网贷不能为空",
    trigger: "change"
  }],
  loveCountry: [{
    required: true,
    message: "爱国爱党、遵纪守法不能为空",
    trigger: "change"
  }],
  aggresively: [{
    required: true,
    message: "积极上进、产业发展意志强烈不能为空",
    trigger: "change"
  }],
  familyRship: [{
    required: true,
    message: "家庭关系、邻里关系和谐不能为空",
    trigger: "change"
  }],
  inteConcept: [{
    required: true,
    message: "具有诚信观念、说话算话不能为空",
    trigger: "change"
  }],
  remark: [{
    required: true,
    message: "请输入其他说明",
    trigger: "blur"
  }]
});

// 提交
const handleSubmit = async () => {
  if(active.value == 0) {
    await ruleFormRef1.value.validate(async (valid, fields) => {
      if (valid) {
        active.value = (from.gamble === "N" && from.drugRelated === "N" && from.whoring === "N" && from.black === "N" && from.privateLending === "N" && from.onlineLoan === "N") ? "1" : "0";
        if (active.value == 0) {
          // 直接进行添加
          await submit()
        }else {
          active.value = 1;
        }
      }
    })
  }else if(active.value == 1) {
    await ruleFormRef1.value.validate(async (valid, fields) => {
      if (valid) {
        if (active.value == 1) {
          // 直接进行添加
          await submit()
        }
      }
    })
  }
};

// 提交数据
const submit = async () => {
  await reqScoring(from).then((req: reqBaseData) => {
    if(req.code == 200){
      proxy.$modal.msgSuccess("评议成功！")
      dialogVisible.value = false;
      $emit("getList")
    } else {
      proxy.$modal.msgError("评议失败，请稍后重试！")
    }
  }).catch(() => {})
}

defineExpose({
  show
});
</script>


<template>

  <el-dialog width="50%" v-model="dialogVisible" @close="close">
    <template #title>
      <span class="el-dialog__title">{{ title }}</span>
    </template>
    <el-form ref="ruleFormRef1" :model="from" v-show="active == 0" :rules="rules" size="default" label-width="100px"
             label-position="top">
      <el-form-item label="姓名">
        <el-tag  class="ml-2" type="success">{{ showInfo.hzName }}</el-tag>
      </el-form-item>
      <el-form-item label="身份证号码">
        <el-tag  class="ml-2" type="success">{{ showInfo.hzIdCard }}</el-tag>
      </el-form-item>
      <el-form-item label="家庭住址">
        <el-tag  class="ml-2" type="success">{{ showInfo.famAddress }}</el-tag>
      </el-form-item>

      <el-form-item label="本人及家庭成员是否有赌博行为" prop="gamble">
        <el-radio-group v-model="from.gamble">
          <el-radio v-for="(item, index) in dict.type.base_yes_no"
                    :key="index"
                    :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="本人及家庭成员是否有涉毒行为" prop="drugRelated">
        <el-radio-group v-model="from.drugRelated">
          <el-radio v-for="(item, index) in dict.type.base_yes_no" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="本人及家庭成员是否有嫖娼行为" prop="whoring">
        <el-radio-group v-model="from.whoring">
          <el-radio v-for="(item, index) in dict.type.base_yes_no" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="本人及家庭成员是否有涉黑行为" prop="black">
        <el-radio-group v-model="from.black">
          <el-radio v-for="(item, index) in dict.type.base_yes_no" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="本人及家庭成员是否有参有民间借贷" prop="privateLending">
        <el-radio-group v-model="from.privateLending">
          <el-radio v-for="(item, index) in dict.type.base_yes_no" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="本人及家庭成员是否有网贷" prop="onlineLoan">
        <el-radio-group v-model="from.onlineLoan">
          <el-radio v-for="(item, index) in dict.type.base_yes_no" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <el-form ref="ruleFormRef2" :model="from" v-show="active == 1" :rules="rules" size="default" label-width="100px"
             label-position="top">
      <el-form-item label="爱国爱党、遵纪守法" prop="loveCountry">
        <el-radio-group v-model="from.loveCountry">
          <el-radio v-for="(item, index) in dict.type.pea_score_grade" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="积极上进、产业发展意志强烈" prop="aggresively">
        <el-radio-group v-model="from.aggresively">
          <el-radio v-for="(item, index) in dict.type.pea_score_grade" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="家庭关系、邻里关系和谐" prop="familyRship">
        <el-radio-group v-model="from.familyRship">
          <el-radio v-for="(item, index) in dict.type.pea_score_grade" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="具有诚信观念、说话算话" prop="inteConcept">
        <el-radio-group v-model="from.inteConcept">
          <el-radio v-for="(item, index) in dict.type.pea_score_grade" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="其他说明" prop="remark">
        <el-input v-model="from.remark" type="textarea" placeholder="请输入其他说明"
                  :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="info" @click="active = 0" v-show="active == 1">上一步</el-button>
      <el-button type="success" @click="handleSubmit">提交</el-button>
      <el-button type="warning" @click="dialogVisible = false">取消</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
export default {
  dicts: ["base_yes_no", "pea_score_grade"]
};
</script>

<style scoped lang="scss">
@import "../index";
</style>
